package cn.shiwei.basic.service.impl;

import cn.shiwei.basic.mapper.BaseMapper;
import cn.shiwei.basic.query.BaseQuery;
import cn.shiwei.basic.service.IBaseService;
import cn.shiwei.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public T findById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    public Integer queryTotal(BaseQuery query) {
        return baseMapper.queryTotal(query);
    }

    @Override
    public PageList<T> queryDate(BaseQuery query) {
        Integer total = baseMapper.queryTotal(query);
        List<T> data = baseMapper.queryDate(query);
        PageList<T> pageList = new PageList<>(total ,data);
        return pageList;
    }

    @Override
    @Transactional
    public void patchRemove(List<Long> ids) {
        baseMapper.patchRemove(ids);
    }
}
